package io.lkt.modules.sys.controller;

import com.google.common.collect.Lists;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BBusinessDetailsEntity;
import io.lkt.modules.sys.entity.BBusinessInfoEntity;
import io.lkt.modules.sys.entity.BBusinessLecturerEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商学院课程信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-10-23 18:17:51
 */
@RestController
@RequestMapping("/sys/bbusinessinfo")
public class BBusinessInfoController {
    @Autowired
    private BBusinessInfoService bBusinessInfoService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private BBusinessLecturerService bBusinessLecturerService;

    @Autowired
    private BBusinessDetailsService bBusinessDetailsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bbusinessinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bBusinessInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/queryList")
//    @RequiresPermissions("sys:bbusinessinfo:list")
    public R queryList(@RequestParam Map<String, Object> params) {
        PageUtils page = bBusinessInfoService.queryPage(params);
        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BBusinessInfoEntity> bBusinessInfoList = Lists.newArrayList();

            for (Object temp : list) {
                BBusinessInfoEntity bBusinessInfoEntity = (BBusinessInfoEntity) temp;
                //公众号名称
                if ((bBusinessInfoEntity.getGzhCode() != null) && (!bBusinessInfoEntity.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.queryGzhCode(bBusinessInfoEntity.getGzhCode());
                    bBusinessInfoEntity.setGzhName(sysGzh.getGzhName());
                } else {
                    bBusinessInfoEntity.setGzhName("全部平台");
                }

                //讲师
                if ((bBusinessInfoEntity.getAuthorId() != null) && (!bBusinessInfoEntity.getAuthorId().equals(""))) {
                    BBusinessLecturerEntity bBusinessLecturer = bBusinessLecturerService.getById(bBusinessInfoEntity.getAuthorId());
                    bBusinessInfoEntity.setAuthorName(bBusinessLecturer.getAuthorName());
                }
//            //作者
//            if ((bBusinessInfoEntity.getUserId() != null) && (!bBusinessInfoEntity.getUserId().equals(""))) {
//                BUserInfoEntity bUserInfo = bUserInfoService.getById(bBusinessInfoEntity.getUserId());
//                bBusinessInfoEntity.setUserName(bUserInfo.getNickName());
//            }

                bBusinessInfoList.add(bBusinessInfoEntity);
            }
            page.setList(bBusinessInfoList);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bbusinessinfo:info")
    public R info(@PathVariable("id") Integer id) {
        BBusinessInfoEntity bBusinessInfo = bBusinessInfoService.getById(id);
        Integer pathType = 0;
        //公众号标识
        if (bBusinessInfo.getGzhCode() == null || bBusinessInfo.getGzhCode().equals("")) {
            bBusinessInfo.setGzhCode("-1");
        }
        //讲师
        Integer authorId = bBusinessInfo.getAuthorId();
        if (authorId != null) {
            BBusinessLecturerEntity bBusinessLecturer = bBusinessLecturerService.getById(authorId);
            if (bBusinessLecturer != null) {
                bBusinessInfo.setAuthorName(bBusinessLecturer.getAuthorName());
            }
        }
        //课程地址
        BBusinessDetailsEntity bBusinessDetails = new BBusinessDetailsEntity();
        bBusinessDetails.setBusinessId(id);
        //通过课程id查询关联的课程小节实体
        BBusinessDetailsEntity bBusinessDetailsEntity = bBusinessDetailsService.queryOne(bBusinessDetails);
        if(bBusinessDetailsEntity.getPath() != null){
            bBusinessInfo.setPath(bBusinessDetailsEntity.getPath());

            if(bBusinessDetailsEntity.getPath().contains(".mp3")){
                pathType = 3;
            }
            if(bBusinessDetailsEntity.getPath().contains(".mp4")){
                pathType = 4;
            }
        }
        if(bBusinessDetailsEntity.getTitle() != null){
            bBusinessInfo.setPathTitle(bBusinessDetailsEntity.getTitle());
        }
        if(bBusinessDetailsEntity.getContent() != null){
            bBusinessInfo.setPathContent(bBusinessDetailsEntity.getContent());
        }

        bBusinessInfo.setPathType(pathType);

        return R.ok().put("bBusinessInfo", bBusinessInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bbusinessinfo:save")
    public R save(@RequestBody BBusinessInfoEntity bBusinessInfo) {

        if (bBusinessInfo.getGzhCode() == null || bBusinessInfo.getGzhCode().equals("") || bBusinessInfo.getGzhCode().equals("-1")) {
            bBusinessInfo.setGzhCode("");
        }

        String authorName = bBusinessInfo.getAuthorName();
        if (authorName != null) {
            BBusinessLecturerEntity bBusinessLecturer = new BBusinessLecturerEntity();
            bBusinessLecturer.setAuthorName(authorName);
            BBusinessLecturerEntity bBusinessLecturerInfo = bBusinessLecturerService.queryOne(bBusinessLecturer);
            if (bBusinessLecturerInfo.getId() != null) {
                bBusinessInfo.setAuthorId(bBusinessLecturerInfo.getId());
            }
        }
        bBusinessInfo.setCreateTime(new Date());
        bBusinessInfo.setUpdateTime(new Date());
        bBusinessInfoService.save(bBusinessInfo);

        //保存课程详情
        BBusinessDetailsEntity bBusinessDetails = new BBusinessDetailsEntity();
        bBusinessDetails.setBusinessId(bBusinessInfo.getId());
        bBusinessDetails.setTitle(bBusinessInfo.getPathTitle());
        bBusinessDetails.setContent(bBusinessInfo.getPathContent());
        bBusinessDetails.setPath(bBusinessInfo.getPath());
        bBusinessDetails.setPathTime(bBusinessInfo.getPathTime());
        if(bBusinessInfo.getPath().contains(".mp3")){
            bBusinessDetails.setType(3);
        }
        if(bBusinessInfo.getPath().contains(".mp4")){
            bBusinessDetails.setType(4);
        }

        bBusinessDetails.setCreateTime(new Date());
        bBusinessDetails.setUpdateTime(new Date());

        if(bBusinessInfo.getPathTitle() == null || bBusinessInfo.getPathTitle().trim().equals("")){
            bBusinessDetails.getTitle();
        }
        if(bBusinessInfo.getPathContent() == null || bBusinessInfo.getPathContent().trim().equals("")){
            bBusinessDetails.getContent();
        }

        bBusinessDetailsService.save(bBusinessDetails);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bbusinessinfo:update")
    public R update(@RequestBody BBusinessInfoEntity bBusinessInfo) {
        ValidatorUtils.validateEntity(bBusinessInfo);

        if (bBusinessInfo.getGzhCode() == null || bBusinessInfo.getGzhCode().equals("") || bBusinessInfo.getGzhCode().equals("-1")) {
            bBusinessInfo.setGzhCode("");
        }

        String authorName = bBusinessInfo.getAuthorName();
        if (authorName != null) {
            BBusinessLecturerEntity bBusinessLecturer = new BBusinessLecturerEntity();
            bBusinessLecturer.setAuthorName(authorName);
            BBusinessLecturerEntity bBusinessLecturerInfo = bBusinessLecturerService.queryOne(bBusinessLecturer);
            if (bBusinessLecturerInfo.getId() != null) {
                bBusinessInfo.setAuthorId(bBusinessLecturerInfo.getId());
            }
        }

        bBusinessInfo.setUpdateTime(new Date());
        bBusinessInfoService.updateById(bBusinessInfo);

        //保存课程详情
        BBusinessDetailsEntity bBusinessDetails = new BBusinessDetailsEntity();
        bBusinessDetails.setBusinessId(bBusinessInfo.getId());
        bBusinessDetails.setTitle(bBusinessInfo.getPathTitle());
        bBusinessDetails.setContent(bBusinessInfo.getPathContent());
        bBusinessDetails.setPath(bBusinessInfo.getPath());
        if(bBusinessInfo.getPath().contains(".mp3")){
            bBusinessDetails.setType(3);
        }
        if(bBusinessInfo.getPath().contains(".mp4")){
            bBusinessDetails.setType(4);
        }
        bBusinessDetails.setPathTime(bBusinessInfo.getPathTime());
        bBusinessDetails.setUpdateTime(new Date());
        bBusinessDetailsService.updateByBusinessId(bBusinessDetails);

        if(bBusinessInfo.getPathTitle() == null || bBusinessInfo.getPathTitle().trim().equals("")){
            bBusinessDetails.getTitle();
        }
        if(bBusinessInfo.getPathContent() == null || bBusinessInfo.getPathContent().trim().equals("")){
            bBusinessDetails.getContent();
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bbusinessinfo:delete")
    public R delete(@RequestBody Integer[] ids) {
        bBusinessInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
